/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cong.javadanang.model.dao.impl;

import cong.javadanang.model.dao.AnswerDAO;
import cong.javadanang.model.entity.Answer;
import hibernate.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Pham
 */
public class AnswerDAOImpl implements AnswerDAO{

    private static AnswerDAOImpl answerDao;

    public static AnswerDAOImpl getInstance() {
        if (answerDao == null) {
            answerDao = new AnswerDAOImpl();
        }
        return answerDao;
    }
    private Session session;
    private Transaction ts;
    
    @Override
    public Answer getAnswerById(int answerId) {
        Answer answer = null;
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            answer = (Answer) session.get(Answer.class, answerId);
            ts.commit();
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
        }finally{
            session.close();
        }
        return answer;
    }

    @Override
    public List<Answer> getAllAnswers() {
        session = HibernateUtil.getSessionFactory().openSession();
        List<Answer> answers = null;
        try {
            ts = session.beginTransaction();
            String sql = "FROM Answer";
            Query query = session.createQuery(sql);
            answers = query.list();
            ts.commit();
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
        }finally{
            session.close();
        }
        return answers;
    }

    @Override
    public boolean createAnswer(Answer answer) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            session.save(answer);
            ts.commit();
            return true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean updateAnswer(Answer answer) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            session.update(answer);
            ts.commit();
            return true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean deleteAnswer(Answer answer) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            session.delete(answer);
            ts.commit();
            return true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean removeAnswer(Answer answer) {
        session = HibernateUtil.getSessionFactory().openSession();
         try {
            ts = session.beginTransaction();
            String sql = "UPDATE Answer set active = false WHERE answerId = :answerId";
            Query query = session.createQuery(sql);
            query.setParameter("answerId", answer.getAnswerId());
            query.executeUpdate();
            ts.commit();
            return true;
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean restoreAnswer(Answer answer) {
        session = HibernateUtil.getSessionFactory().openSession();
         try {
            ts = session.beginTransaction();
            String sql = "UPDATE Answer set active = true WHERE answerId = :answerId";
            Query query = session.createQuery(sql);
            query.setParameter("answerId", answer.getAnswerId());
            query.executeUpdate();
            ts.commit();
            return true;
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }
    
}
